#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>

#import "MapApp.h"

@implementation MapApp

- (void)applicationDidFinishLaunching:(NSNotification*)aNotification
{
  int i, j;

  // window
  window = [[UIWindow alloc] initWithContentRect:CGRectMake(0, 20, 320, 460)];

  // main view
  mainView = [[UIView alloc] initWithFrame: [window bounds]];
  CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  float color[4] = {0, 0, 0, 1};
  [mainView setBackgroundColor:CGColorCreate(colorSpace, color)];

  // navigation bar
  navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 48)];
  [navigationBar showLeftButton:@"Reset" withStyle:0 rightButton:nil withStyle:0];
  [navigationBar setBarStyle:0];
  [navigationBar setDelegate:self];
  
  // navi title
  UINavigationItem *title = [[[UINavigationItem alloc] initWithTitle:@"Shake Me !"] autorelease];
  [navigationBar pushNavigationItem:title];

  // transition view
  transitionView = [[UITransitionView alloc] initWithFrame:CGRectMake(70, 140, 180, 180)];

  // map views
  for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {
      mapViews[i][j] = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 180, 180)];
      [mapViews[i][j] setImage:[[UIImage applicationImageNamed:[NSString stringWithFormat:@"%d-%d.png", i, j]] autorelease]]; 
    }
  }

  // focus view
  focusView = [[UIImageView alloc] initWithFrame:CGRectMake(70 + 70, 140 + 70, 40, 40)];
  [focusView setImage:[[UIImage applicationImageNamed:@"focus.png"] autorelease]];
  
  // set window
  [mainView addSubview:transitionView];
  [mainView addSubview:focusView];
  [mainView addSubview:navigationBar];
  [transitionView autorelease];
  [focusView autorelease];
  [navigationBar autorelease];

  [window setContentView:mainView];
  [mainView autorelease];

  [window orderFront:self];
  [window makeKey:self];
  [window _setHidden:NO];

  indexX = indexY = 1;
  [transitionView transition:0 toView:mapViews[indexX][indexY]];

  threshold = 0.3;
  isAnimating = NO;
}

- (void)applicationWillTerminate {
  [window release];
}

- (void)acceleratedInX:(float)x Y:(float)y Z:(float)z;
{
  if (isAnimating || [transitionView isTransitioning]) {
    return;
  }

  if (x*x > y*y) {
    if (x > threshold) {
      if (indexX == 2) {
        [self endOfMap:1:0];
      } else {
        indexX = (indexX + 1) % 3;
        [transitionView transition:1 toView:mapViews[indexX][indexY]];
      }
    } else if (x < threshold * -1) {
      if (indexX == 0) {
        [self endOfMap:-1:0];
      } else {
        indexX = (indexX + 2) % 3;
        [transitionView transition:2 toView:mapViews[indexX][indexY]];
      }
    }
  } else {
    if (y > threshold) {
      if (indexY == 2) {
        [self endOfMap:0:1];
      } else {
        indexY = (indexY + 1) % 3;
        [transitionView transition:3 toView:mapViews[indexX][indexY]];
      }
    } else if (y < threshold * -1) {
      if (indexY == 0) {
        [self endOfMap:0:-1];
      } else {
        indexY = (indexY + 2) % 3;
        [transitionView transition:7 toView:mapViews[indexX][indexY]];
      }
    }
  }
}

- (void)navigationBar:(UINavigationBar*)navbar buttonClicked:(int)button {
  indexX = indexY = 1;
  isAnimating = YES;

  [transitionView setFrame:CGRectMake(70, 140, 0, 0)];
  [UIView beginAnimations:nil]; 
  [transitionView transition:7 toView:mapViews[indexX][indexY]];
  [UIView setAnimationDuration:1.0]; 
  [transitionView setFrame:CGRectMake(70, 140, 180, 180)];
  [UIView endAnimations];

  [self performSelector:@selector(endAnimation) object:nil afterDelay:1.0];
}

- (void)endAnimation {
  [mapViews[indexX][indexY] setFrame:CGRectMake(0, 0, 180, 180)];
  isAnimating = NO;
}

- (void)endOfMap:(int)x:(int)y {
  isAnimating = YES;

  [UIView beginAnimations:nil]; 
  [UIView setAnimationDuration:0.2]; 
  [mapViews[indexX][indexY] setFrame:CGRectMake(-30 * x, -30 * y, 180, 180)];
  [UIView endAnimations];

  [self performSelector:@selector(endAnimation) object:nil afterDelay:0.3];
}

@end

